package vn.appsmobi.ulti;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import java.util.HashSet;

public abstract class PackageMonitor extends BroadcastReceiver
{
  static final IntentFilter sExternalFilt;
  static final IntentFilter sNonDataFilt;
  static final IntentFilter sPackageFilt = new IntentFilter();
  String[] mAppearingPackages;
  int mChangeType;
  String[] mDisappearingPackages;
  String[] mModifiedPackages;
  boolean mSomePackagesChanged;
  String[] mTempArray = new String[1];
  final HashSet<String> mUpdatingPackages = new HashSet<String>();

  static
  {
    sNonDataFilt = new IntentFilter();
    sExternalFilt = new IntentFilter();
    sPackageFilt.addAction("android.intent.action.PACKAGE_ADDED");
    sPackageFilt.addAction("android.intent.action.PACKAGE_REMOVED");
    sPackageFilt.addAction("android.intent.action.PACKAGE_CHANGED");
    sPackageFilt.addAction("android.intent.action.QUERY_PACKAGE_RESTART");
    sPackageFilt.addAction("android.intent.action.PACKAGE_RESTARTED");
    sPackageFilt.addAction("android.intent.action.UID_REMOVED");
    sPackageFilt.addDataScheme("package");
    sNonDataFilt.addAction("android.intent.action.UID_REMOVED");
    sExternalFilt.addAction("android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE");
    sExternalFilt.addAction("android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE");
  }

  String getPackageName(Intent itent)
  {
    Uri uri = itent.getData();
    if (uri != null);
    	String str = uri.getSchemeSpecificPart();
      return str;
  }

  public void onBeginPackageChanges()
  {
  }

  public void onFinishPackageChanges()
  {
  }

  public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit)
  {
    return false;
  }

  public void onPackageAdded(String packageName, int uid)
  {
  }

  public void onPackageAppeared(String packageName, int reason)
  {
  }

  public void onPackageChanged(String packageName, int uid, String[] components)
  {
  }

  public void onPackageDisappeared(String packageName, int reason)
  {
  }

  public void onPackageModified(String packageName) 
  {
  }

  public void onPackageRemoved(String packageName, int uid)
  {
  }

  public void onPackageUpdateFinished(String packageName, int uid)
  {
  }

  public void onPackageUpdateStarted(String packageName, int uid)
  {
  }

  public void onPackagesAvailable(String[] packages)
  {
  }

  public void onPackagesUnavailable(String[] packages)
  {
  }

  public void onReceive(Context context, Intent intent)
  {
    onBeginPackageChanges();
    this.mAppearingPackages = null;
    this.mDisappearingPackages = null;
    this.mSomePackagesChanged = false;
    String action = intent.getAction();
    String packagename = null;
    int uid;
    if ("android.intent.action.PACKAGE_ADDED".equals(action))
    {
      packagename = getPackageName(intent);
      uid = intent.getIntExtra("android.intent.extra.UID", 0);
      this.mSomePackagesChanged = true;
      if (packagename != null)
      {
        this.mAppearingPackages = this.mTempArray;
        this.mTempArray[0] = packagename;
        if (!intent.getBooleanExtra("android.intent.extra.REPLACING", false))
        {
        	this.mChangeType = 3;
        	onPackageAdded(packagename, uid);
        }
        	
        this.mModifiedPackages = this.mTempArray;
        this.mChangeType = 1;
        onPackageUpdateFinished(packagename, uid);
        onPackageModified(packagename);
        onPackageAppeared(packagename, this.mChangeType);
        if (this.mChangeType != 1);
      }
    }
    else
    {
      synchronized (this.mUpdatingPackages)
      {
        this.mUpdatingPackages.remove(packagename);
        if (this.mSomePackagesChanged)
          onSomePackagesChanged();
        onFinishPackageChanges();
       
      }
      if ("android.intent.action.PACKAGE_REMOVED".equals(action))
      {
        packagename = getPackageName(intent);
        uid = intent.getIntExtra("android.intent.extra.UID", 0);
        if (packagename != null)
        {
          this.mDisappearingPackages = this.mTempArray;
          this.mTempArray[0] = packagename;
          if (intent.getBooleanExtra("android.intent.extra.REPLACING", false))
            this.mChangeType = 1;
          else
          {
            synchronized (this.mUpdatingPackages)
            {
              onPackageUpdateStarted(packagename, uid);
            }         
            this.mChangeType = 3;
            this.mSomePackagesChanged = true;
            onPackageRemoved(packagename, uid);
          }
        }
      }
      else if ("android.intent.action.PACKAGE_CHANGED".equals(action))
      {
        packagename = getPackageName(intent);
        uid = intent.getIntExtra("android.intent.extra.UID", 0);
        String[] component_list = intent.getStringArrayExtra("android.intent.extra.changed_component_name_list");
        if (packagename != null)
        {
          this.mModifiedPackages = this.mTempArray;
          this.mTempArray[0] = packagename;
          onPackageChanged(packagename, uid, component_list);
          onPackageModified(packagename);
        }
      }
      else if ("android.intent.action.QUERY_PACKAGE_RESTART".equals(action))
      {
        this.mDisappearingPackages = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
        this.mChangeType = 2;
        if (onHandleForceStop(intent, this.mDisappearingPackages, intent.getIntExtra("android.intent.extra.UID", 0), false))
          setResultCode(-1);
      }
      else if ("android.intent.action.PACKAGE_RESTARTED".equals(action))
      {
        String[] arrayOfString3 = new String[1];
        packagename = getPackageName(intent);
        this.mDisappearingPackages = arrayOfString3;
        this.mChangeType = 2;
        onHandleForceStop(intent, this.mDisappearingPackages, intent.getIntExtra("android.intent.extra.UID", 0), true);
      }
      else if ("android.intent.action.UID_REMOVED".equals(action))
      {
        onUidRemoved(intent.getIntExtra("android.intent.extra.UID", 0));
      }
      else if ("android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE".equals(action))
      {
        String[] pakage_list = intent.getStringArrayExtra("android.intent.extra.changed_package_list");
        this.mAppearingPackages = pakage_list;
        this.mChangeType = 2;
        this.mSomePackagesChanged = true;
        if (pakage_list != null)
        {
          onPackagesAvailable(pakage_list);
          for (int j = 0; j < pakage_list.length; j++)
            onPackageAppeared(pakage_list[j], 2);
        }
      }
      else if ("android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE".equals(action))
      {
        String[] pakage_list = intent.getStringArrayExtra("android.intent.extra.changed_package_list");
        this.mDisappearingPackages = pakage_list;
        this.mChangeType = 2;
        this.mSomePackagesChanged = true;
        if (pakage_list != null)
        {
          onPackagesUnavailable(pakage_list);
          for (int i = 0; i < pakage_list.length; i++)
            onPackageDisappeared(pakage_list[i], 2);
        }
      }
    }
  }

  public void onSomePackagesChanged()
  {
  }

  public void onUidRemoved(int paramInt)
  {
  }
}
